#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2021/10/18 22:19 
# @File           : schemas.py
# @IDE            : PyCharm
# @desc           : pydantic 模型，用于数据库序列化操作

# pydantic 验证数据：https://blog.csdn.net/qq_44291044/article/details/104693526

import json
from typing import List, Optional
from pydantic import BaseModel, validator
from apps.customer import models
from core.data_types import Telephone, DatetimeStr


class Customer(BaseModel):
    name: str
    nickname: Optional[str] = None
    wechat: Optional[str] = None
    qq: Optional[str] = None
    grade: Optional[str] = "C"
    platform: Optional[int] = 1
    address_code: Optional[str] = None
    telephone: Telephone = None


class CustomerIn(Customer):

    @validator('grade')
    def validate_grade(cls, grade):
        if not grade:
            return grade
        if grade not in models.Customer.GRADE_CHOICES.keys():
            raise ValueError("请选择正确的等级")
        return grade

    @validator('platform')
    def validate_platform(cls, platform):
        if not platform:
            return platform
        if platform not in models.Customer.PLATFORM_CHOICES.keys():
            raise ValueError("请选择正确的平台")
        return platform

    class Config:
        # 示例参数值，会默认显示在接口文档中，example为固定写法
        schema_extra = {
            "example": {
                "name": "客户名",
                "nickname": "客户昵称",
                "telephone": "13459782158",
                "wechat": "weixinhao",
                "qq": "123456",
                "address_code": json.dumps(["130000", "130100", "130101"]),
                "grade": "C",
                "platform": 1,
            }
        }


class CustomerSimpleOut(Customer):
    id: int
    joined_time: DatetimeStr
    update_time: DatetimeStr

    class Config:
        orm_mode = True


class CustomerDel(BaseModel):
    ids: List[int]
